package com.jming.jike.controller.front;

import com.jming.jike.common.ResponseResult;
import com.jming.jike.common.ValidatorGroup.Insert;
import com.jming.jike.dto.front.AddressBookDTO;
import com.jming.jike.service.front.AddressBookService;
import com.jming.jike.vo.TreeNode;
import com.jming.jike.vo.front.AddressBookVO;
import com.jming.jike.vo.front.RegionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2022/05/13
 */
@RestController
@RequestMapping("/addressBook")
@Api(tags = "用户地址管理相关接口", value = "/addressBook")
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;

    @PostMapping("/insert")
    @ApiOperation(value = "新增地址", notes = "新增地址", produces = "application/json", httpMethod = "POST")
    public ResponseResult<Boolean> insert(@RequestBody @Validated(Insert.class) AddressBookDTO addressBookDTO) {

        return ResponseResult.success(addressBookService.insert(addressBookDTO));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改地址信息", notes = "修改地址信息", produces = "application/json", httpMethod = "PUT")
    public ResponseResult<Boolean> update(@RequestBody @Validated(Insert.class) AddressBookDTO addressBookDTO) {

        return ResponseResult.success(addressBookService.update(addressBookDTO));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除地址", notes = "删除地址", produces = "application/json", httpMethod = "DELETE")
    public ResponseResult<Boolean> delete(@PathVariable("id") Long id) {

        return ResponseResult.success(addressBookService.delete(id));
    }

    @GetMapping("/queryList")
    @ApiOperation(value = "查询地址列表", notes = "查询地址列表", produces = "application/json", httpMethod = "GET")
    public ResponseResult<List<AddressBookVO>> queryList() {

        return ResponseResult.success(addressBookService.queryList());
    }

    @GetMapping("/addressBookDetail/{id}")
    @ApiOperation(value = "查询地址详情", notes = "查询地址详情", produces = "application/json", httpMethod = "GET")
    public ResponseResult<AddressBookVO> addressBookDetail(@PathVariable("id") Long id) {

        return ResponseResult.success(addressBookService.addressBookDetail(id));
    }

    @GetMapping("/getRegionTree")
    @ApiOperation(value = "获取省市区树列表", notes = "获取省市区树列表", produces = "application/json", httpMethod = "GET")
    public ResponseResult<List<TreeNode<RegionVO>>> getRegionTree() {

        return ResponseResult.success(addressBookService.getRegionTree());
    }

}
